<% @title = "RubyGems.org Data Dumps" %>

<div class="t-body">
  <p>We provide weekly dumps of the RubyGems.org PostgreSQL data. This dump is sanitized, removing all user information.</p>
  <p>The <a href="https://github.com/rubygems/rubygems.org/tree/master/script/load-pg-dump">load-pg-dump</a> script is
     available as an example of how to to download and load the most recent weekly dump.</p>
  <ul class="rubygems-dump-listing-postgresql"></ul>
  <p>We also provide weekly dumps of the historial RubyGems.org Redis data. (We do not use redis anymore but these are here for historical purposes.)</p>
  <ul class="rubygems-dump-listing-redis"></ul>
</div>

<% content_for :javascript do %>
  <script type="text/javascript">
    $(document).ready(function() {

      var getDumpData = function(target, type) {
        return $.get('https://s3-us-west-2.amazonaws.com/rubygems-dumps/?prefix=production/public_' + type).done(function(data) {
          var files, xml;
          xml = $(data);
          files = parseS3Listing(xml);
          files = sortByLastModified(files);
          $(target).html(renderDumpList(files));
        }).fail(function(error) {
          console.error(error);
        });
      };

      var parseS3Listing = function(xml) {
        var files;
        files = $.map(xml.find('Contents'), function(item) {
          item = $(item);
          return {
            Key: item.find('Key').text(),
            LastModified: item.find('LastModified').text(),
            Size: item.find('Size').text(),
            StorageClass: item.find('StorageClass').text()
          };
        });
        return files;
      };

      var sortByLastModified = function(files) {
        return files.sort(function(a, b) {return Date.parse(b.LastModified) - Date.parse(a.LastModified)});
      };

      var bytesToSize = function(bytes) {
        var i, k, sizes;
        if (bytes === 0) {
          return '0 Byte';
        }
        k = 1024;
        sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
        i = Math.floor(Math.log(bytes) / Math.log(k));
        return (bytes / Math.pow(k, i)).toPrecision(3) + " " + sizes[i];
      };

      var renderDumpList = function(files) {
        var content;
        content = [];
        jQuery.each(files, function(idx, item) {
          if ('STANDARD' === item.StorageClass) {
            return content.push("<li><a href='https://s3-us-west-2.amazonaws.com/rubygems-dumps/" + item.Key + "'>" + (item.LastModified.replace('.000Z', '')) + " (" + (bytesToSize(item.Size)) + ")</a></li>");
          }
        });
        return content.join("\n");
      };

      getDumpData('ul.rubygems-dump-listing-postgresql', 'postgresql');
      getDumpData('ul.rubygems-dump-listing-redis', 'redis');
    });
  </script>
<% end %>
